/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdesk;

import java.util.ArrayList;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;

/**
 * Represents a help ticket's or user's area of expertise, such as printers.
 *
 * @author brian
 */
public class Expertise {
    static ArrayList<Expertise> expertises;
    int id;
    String name;

    /**
     * Get a unique id representing this expertise.
     * @return
     */
    public int getId()
    {
        return id;
    }

    /**
     * Get the name of this expertise, such as 'Macs'
     * @return
     */
    public String getName()
    {
        return name;
    }

    /**
     * Get all expertises, possibly from the database. This value is cached,
     * so the any changes to the expertise table will require an application
     * restart. Fortunately, this shouldn't happen to often.
     *
     * @return
     * @throws SQLException
     */
    static public ArrayList<Expertise> getExpertises() throws SQLException
    {
        if (expertises == null)
            fetchExpertises();
        return expertises;
    }

    /**
     * Used for getting the expertise from the database.
     * 
     * @throws SQLException
     */
    static private void fetchExpertises() throws SQLException
    {
        Connection conn = null;
        Statement stmt;
        ResultSet rs;
        ArrayList<Expertise> exp = new ArrayList<Expertise>(32);
        
        try
        {
            conn = DBManager.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT expertiseid, name FROM expertise");

            while (rs.next())
            {
                Expertise ex = new Expertise();
                ex.id = rs.getInt(1);
                ex.name = rs.getString(2);
                exp.add(ex);
            }

            expertises = exp;
        }
        finally
        {
            if (conn != null)
                conn.close();
        }
    }
}
